<template>
    <div>
        <van-nav-bar id="reset" title="申请列表" left-arrow @click-left="backHandle" fixed z-index="100" />

        <div class="pageCon fuPage" id="box">
            <div class="listBox">
                <div class="listTop listTopDownMenu">
                    <van-dropdown-menu>
                        <van-dropdown-item v-model="selectType" :options="typeList" @change="onSelectType" />
                        <van-dropdown-item v-model="selectStatus" :options="statusList" @change="onSelectStatus" />
                    </van-dropdown-menu>
                </div>

                <div class="listMain">
                    <template v-if="historyList.length != 0">
                        <div class="itemBox" v-for="(item, index) in historyList" :key="index">
                            <div class="orderNumTip">
                                申请编号：{{ item.orderNumber }}
                                <span v-if="item.orderStatus == 0">
                                    审核人审核中
                                </span>
                                <span v-if="item.orderStatus == 1" class="passColor">
                                    已审核
                                </span>
                                <span v-if="item.orderStatus == 2" class="resColor">
                                    医助已驳回
                                </span>
                                <span v-if="item.orderStatus == 3">
                                    医助审核中
                                </span>
                                <span v-if="item.orderStatus == 4" class="resColor">
                                    审核人已驳回
                                </span>
                            </div>
                            <div class="line1">

                            </div>
                            <div>
                                <span>{{ item.doctorName }}的{{ item.orderType == 0 ? '加号' : '亲情就诊' }}申请</span>
                                <span>{{ item.createTime }}</span>
                            </div>
                            <div class="imgBox" v-if="item.imgList != null && item.imgList.length != 0">
                                <span>相关图片</span>
                                <div>
                                    <img :src="ele.imgUrl" alt="" v-for="(ele, eIndex) in item.imgList" :key="eIndex"
                                        @click="checkImg(item.imgList, eIndex)">
                                </div>
                            </div>
                            <div class="passRea" v-if="item.orderStatus == 1"> <span>就诊时间：{{ item.visitTime
                                    }}</span>
                            </div>
                            <div class="passRea" v-if="item.orderStatus == 4 || item.orderStatus == 2">
                                驳回原因： {{ item.unPass }}
                            </div>
                            <div class="btnBox" v-if="item.orderStatus == 2">
                                <div class="applyBtn" @click="resHandle(item)">重新提交</div>
                            </div>

                        </div>
                    </template>
                    <div class="emptyBox" v-else>
                        <img src="../../assets/tip_content.png">
                    </div>
                </div>
            </div>
        </div>




        <van-popup v-model="showPop" round>
            <div class="diaBox">
                <div>{{ selectObj.orderType == 0 ? '加号' : '亲情就诊' }}申请</div>
                <div class="infoBox">
                    <div class="infoLine infoLine1" v-if="selectObj.orderType == 1">
                        请上传您的病例等相关图片
                        <div class="imgBox">
                            <div>
                                <van-uploader v-model="fileList" :before-delete="beforeDelete" :after-read="afterRead"
                                    multiple :max-count="9" />
                            </div>
                        </div>
                    </div>
                </div>
                <div class="diaBtnBox">
                    <div @click="closeHandle">取消</div>
                    <div @click="addOrderHandle(orderType)">提交</div>
                </div>
            </div>
        </van-popup>
    </div>

</template>

<script>
import { ImagePreview } from 'vant';
import { listMyApply, unApplyService } from '../../network/user'
export default {
    name: 'HomeR',
    data() {
        return {

            hisFlag: 0, //  1  亲情就诊  0 加号记录

            type: 0,// -1全部  1 已使用 0 未使用

            historyList: [],
            oldList: [],

            showPop: false,

            selectObj: '',

            user: JSON.parse(localStorage.user),
            fileList: [],
            isLoading: false,

            selectStatus: -1,
            selectType: -1,

            statusList: [
                {
                    text: '默认分类',
                    value: -1
                },
                {
                    text: '审核人审核中',
                    value: 0
                },
                {
                    text: '已审核',
                    value: 1
                },
                {
                    text: '医助已驳回',
                    value: 2
                },
                {
                    text: '医助审核中',
                    value: 3
                },
                {
                    text: '审核人已驳回',
                    value: 4
                },
            ],
            typeList: [
                {
                    text: '全部申请',
                    value: -1
                },
                {
                    text: '亲情就诊',
                    value: 1
                },
                {
                    text: '加号申请',
                    value: 0
                }
            ],
        }
    },
    mounted() {

    },
    methods: {

        onSelectStatus(val) {
            console.log(val);
            this.selectStatus = val
            this.historyList = this.oldList.filter(item => {
                if (this.selectStatus == -1 && this.selectType == -1) {
                    return item
                } else if (this.selectStatus == -1) {
                    return item.orderType == this.selectType
                } else if (this.selectType == -1) {
                    return item.orderStatus == this.selectStatus
                } else if (this.selectStatus != -1 && this.selectType != -1) {
                    return item.orderStatus == this.selectStatus && item.orderType == this.selectType
                }
            })


        },

        onSelectType(val) {
            console.log(val);
            this.selectType = val
            this.historyList = this.oldList.filter(item => {
                if (this.selectStatus == -1 && this.selectType == -1) {
                    return item
                } else if (this.selectStatus == -1) {
                    return item.orderType == this.selectType
                } else if (this.selectType == -1) {
                    return item.orderStatus == this.selectStatus
                } else if (this.selectStatus != -1 && this.selectType != -1) {
                    return item.orderStatus == this.selectStatus && item.orderType == this.selectType
                }
            })
        },


        addOrderHandle() {


            if (this.selectObj.orderType == 1 && this.fileList.length == 0) {
                this.$toast('请上传相关图片')
                return
            }

            let fd = new FormData();

            fd.append("orderId", this.selectObj.orderId);
            fd.append("orderNumber", this.selectObj.orderNumber);
            if (this.selectObj.orderType == 1) {
                this.fileList.forEach(ele => {
                    fd.append("file", ele.file);
                });

            }
            unApplyService(fd).then(res => {
                if (res.flag) {
                    this.$toast('提交成功')
                    console.log(res);
                    this.getList()
                    this.fileList = []
                    this.closeHandle()
                    this.selectObj = ''
                }
            })
        },


        resHandle(obj) {

            this.selectObj = obj
            this.showPop = true

        },

        closeHandle() {
            this.showPop = false
        },



        backHandle() {
            this.$router.back()
        },

        getList() {

            listMyApply({
                userId: localStorage.userId,
                // userId: 26,
            }).then(res => {
                if (res.flag) {
                    this.isLoading = false
                    res.data = res.data.filter(item => {
                        return item.orderType != 2
                    })

                    this.historyList = res.data
                    this.oldList = JSON.parse(JSON.stringify(res.data))
                    console.log(this.historyList);
                }
            })

        },

        pullHandle() {
            this.isLoading = true
            this.getList()
        },


        checkImg(list, index) {

            console.log(list);
            var arr = []
            list.forEach(item => {
                arr.push(item.imgUrl)
            })
            console.log(arr);
            ImagePreview({
                images: arr, // 需要预览的图片 URL 数组
                showIndex: true, // 是否显示页码
                closeable: true, // 是否显示关闭图标	
                startPosition: index,
                showIndicators: true
            })
        },


        // 删除图片
        beforeDelete: function (e) {
            console.log(e);

            this.fileList = this.fileList.filter(item => {
                return item.file.name != e.file.name
            })

            console.log(this.fileList);
            console.log(this.file);

            // this.file = [];
            // this.fileList = [];
        },
        // 图片上传
        afterRead: function (file) {
            // 此时可以自行将文件上传至服务器
            this.file = file.file
            console.log(this.file);
        },



    },
    created() {

        this.getList()
    },
    //计算属性
    computed: {

    },
    filters: {

    },
    watch: {
    }

}

</script>

<style lang='less' scoped>
.pageCon {

    padding: 112px 20px 0 !important;
    box-sizing: border-box;
    position: relative;
    width: 100%;
    height: 100vh;
    background-color: #EEF1F7;
    overflow: hidden;
}


.listBox {
    width: 100%;
    height: 100%;
    // background: #fff;
    border-radius: 20px;
    box-shadow: 0px 2px 6px 0px rgba(51, 51, 51, 0.08);
    overflow-y: scroll;
}


.listTop {
    position: sticky;
    top: 0px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    box-sizing: border-box;
    padding: 30px 34px 10px 44px;
    width: 100%;
    font-family: Source Han Sans CN;
    font-weight: 500;
    font-size: 28px;
    color: #333333;
    background: #fff;
    border-radius: 20px;
}

.listTop>div>span:nth-child(2) {
    margin-left: 30px;
    color: #999999;
}

.itemBox {
    margin-bottom: 20px;
    box-sizing: border-box;
    padding: 44px 30px 44px 44px;
    width: 100%;
    background: #fff;
    box-shadow: 0px 2px 6px 0px rgba(51, 51, 51, 0.08);
    border-radius: 20px;
    border-bottom: 2px solid #f2f2f2;
}

.itemBox>div {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    font-family: Source Han Sans CN;
    font-weight: 400;
    font-size: 28px;
    color: #333333;
    line-height: 38px;

}

.itemBox>div:nth-child(2) {
    justify-content: flex-end;
}

.itemBox>div:nth-child(3) {
    margin-top: 12px;
}

.itemBox>div:nth-child(3)>span:last-child {
    font-size: 24px;
    color: #999999;
}

#reset /deep/ .van-nav-bar__left .van-nav-bar__text,
#reset /deep/ .van-nav-bar__left .van-icon {
    color: #333 !important;
}

.emptyBox {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
}

.emptyBox>img {
    margin-top: 400px;
    width: 374px;
    height: 314px;
}

.applyRes {
    margin-top: 12px;
    justify-content: start !important;
    font-size: 24px !important;
    color: #666 !important;
}

.applyRes>span {
    color: #333 !important;
}


.passRea {
    padding-top: 24px;
    border-top: 1px solid #f2f2f2;
    font-size: 28px !important;
    color: #999 !important;
}

.btnBox {
    margin-top: 24px;
    display: flex;
    align-items: center !important;
    justify-content: flex-end !important;

    width: 100%;
}

.imgBox {
    margin-top: 24px;
    display: flex;
    flex-direction: column;
    align-items: flex-start !important;
    padding-top: 24px;
    border-top: 1px solid #f2f2f2;
    width: 100%;
}

.imgBox>span {
    font-size: 28px;
    color: #999;
}

.imgBox>div>img {
    margin: 16px 16px 16px 0;
    width: 100px;
    height: 100px;
    // background: aqua;
    border-radius: 16px;
}


.resColor {
    color: #F56C6C !important;
}

.passColor {
    color: #67C23A !important;
}

.applyBtn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 136px;
    height: 48px;
    border-radius: 24px;
    border: 1px solid rgba(27, 178, 106, .5);
    font-family: Source Han Sans CN;
    font-weight: bold;
    font-size: 20px;
    color: #1BB26A !important;
}

.orderNumTip {
    margin-bottom: 12px;
    width: 100%;
    font-size: 24px;
    color: #333;
}



.btnBox {
    margin-top: 24px;
    display: flex;
    align-items: center !important;
    justify-content: flex-end !important;

    width: 100%;
}

.applyBtn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 136px;
    height: 48px;
    border-radius: 24px;
    border: 1px solid rgba(27, 178, 106, .5);
    font-family: Source Han Sans CN;
    font-weight: bold;
    font-size: 20px;
    color: #1BB26A !important;
}



.diaBox {
    width: 642px;
    // height: 362px;
    background: #FFFFFF;
    border-radius: 20px;
    font-family: Source Han Sans CN;

}

.diaBox>div:first-child {
    width: 100%;
    height: 110px;
    border-bottom: 2px solid #f2f2f2;
    text-align: center;
    font-weight: bold;
    font-size: 34px;
    color: #333333;
    line-height: 110px;
}

.infoBox {
    margin: 40px auto;
    box-sizing: border-box;
    padding: 18px 38px;
    width: 578px;
    // height: 280px;
    border-radius: 16px;
    border: 1px solid rgba(204, 204, 204, .5);

}

.infoLine {
    display: flex;
    // align-items: center;
    flex-direction: column;
    justify-content: space-between;
    box-sizing: border-box;
    width: 100%;
    padding: 22px 0px;
    font-family: Source Han Sans CN;
    font-weight: 400;
    font-size: 28px;
    color: #334840;
    line-height: 64px;
}

.infoLine>span {
    font-family: Source Han Sans CN;
    font-weight: 400;
    font-size: 28px;
    color: #999999;
    line-height: 42px;
}

.diaBtnBox {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    height: 90px;
    border-top: 2px solid #f2f2f2;
}

.diaBtnBox>div {
    display: flex;
    align-items: center;
    justify-content: center;
    flex: 1;
    height: 100%;
    font-weight: bold;
    font-size: 28px;
    color: #333333;
    line-height: 64px;
}

.diaBtnBox>div:nth-child(2) {
    border-left: 2px solid #f2f2f2;
    color: #275141;
}
</style>